import datetime
import time
from selenium.webdriver.common.by import By
from Utils import TestDriver

class ExamEdit:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/survey/new?mode=exam"
        self.driver.get(self.url)

    def ExamEditNotLogin(self):
        # 未登录状态下，返回到登录界面
        # time.sleep(2)
        # 验证是不是在登录界面
        time.sleep(1)
        url = self.driver.current_url
        assert (url == "http://8.155.1.153:8081/user/login")
        self.driver.find_element(By.CSS_SELECTOR, "#username")
        self.driver.find_element(By.CSS_SELECTOR, "#password")
        # 添加截图
        TestDriver.getScreeShot()

    def ExamYesEditByLogin(self):
        # 进来的时候就是创建考试界面了，要回到主页
        examurl = "http://8.155.1.153:8081/home"
        self.driver.get(examurl)
        self.driver.fullscreen_window()
        # 保存考试后开启有题目的考试 —— 清空标题名，名字设置为有题目的考试，保存，回退两次到主页，再点我的项目，再用find_elemengt选中名字进入，
        # 点击开启考试，点击网址跳转到新窗口，切换窗口，选择选项后点击提交考试
        (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)").click())  # 创建考试
        (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").clear())  # 清空考试名称
        (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").send_keys("有题目的考试"))  # 设置考试名字
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#rc-tabs-1-panel-1 > div > div > dl:nth-child(1) > div > div:nth-child(1) > dd > div").click()  # 添加选择题
        time.sleep(1)
        (self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span").click())  # 保存考试
        self.driver.back()  # 回退到主页
        self.driver.back()  # 回退到主页
        (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li:nth-child(2) > span > a > div > span.anticon.ant-pro-base-menu-item-icon > span > svg").click())  # 点击我的项目
        examnums = self.driver.find_elements(By.CSS_SELECTOR, "span.survey-title")  # 查找
        name_element = None
        for name in examnums:
            if name.text == "有题目的考试":
                name_element = name
                break

        if name_element:
            name_element.click()
        else:
            print("有题目的考试——异常不存在")
            TestDriver.getScreeShot()  # 截图记录
        time.sleep(1)
        # 点击开启考试按钮
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.survey-main-content > div > div > div:nth-child(2) > div:nth-child(2) > div.ant-row > div > div > div:nth-child(2) > div:nth-child(2) > button").click()
        # 点击打开网址按钮
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.survey-main-content > div > div > div:nth-child(2) > div:nth-child(2) > div.ant-row > div > div > div:nth-child(1) > span.ant-input-group-wrapper.ant-input-search.ant-input-search-with-button.open-target > span > span > button > span").click()
        # 出现了新窗口，跳转到新窗口
        curwindow = self.driver.current_window_handle  # 获取当前窗口的句柄
        allwindow = self.driver.window_handles  # 获取所有窗口句柄
        for window in allwindow:
            if window != curwindow:
                self.driver.switch_to.window(window)  # 切换到新的窗口
                break
        # 对新窗口进行操作
        # 点击选项1
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div > div > div.survey-antd-body > div > section > div.survey-antd-body-question-body > div > div > div > div:nth-child(1) > label > span:nth-child(2) > div").click()
        time.sleep(1)
        # 点击提交
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div > div > div.survey-antd-footer > div.survey-antd-footer-pagination > div > button > div").click()
        # root > div > div > div > div.survey-antd-footer > div.survey-antd-footer-pagination > div > button
        time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR, "#result-message").text
        assert (text == "问卷到此结束，感谢您的参与！")
        # 关闭新窗口，获取所有窗口句柄，切换回老窗口
        self.driver.close()
        allwindow = self.driver.window_handles  # 获取所有窗口句柄
        for window in allwindow:
            if window:
                self.driver.switch_to.window(window)
                break
            else:
                print("异常，所有窗口已经关闭")
                TestDriver.getScreeShot()  # 截图记录

    def ExamNoEditByLogin(self):
        # 进来的时候就是创建考试界面了，要回到主页
        examurl = "http://8.155.1.153:8081/home"
        self.driver.get(examurl)
        self.driver.fullscreen_window()
        time.sleep(2)
        try:
            # 登录状态下，异常发布考试：
            # 1.创建考试后不保存考试 —— 清空标题名，名字设置为不保存的考试，回退一次到主页，再点我的项目，再用find_elemengts判断元素不存在
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)").click())  # 创建考试
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").clear())  # 清空考试名称
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").send_keys("不保存的考试"))  # 设置考试名字
            self.driver.back()  # 回退到主页
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li:nth-child(2) > span > a > div > span.anticon.ant-pro-base-menu-item-icon > span > svg").click())  # 点击我的项目
            examnums = self.driver.find_elements(By.CSS_SELECTOR, "span.survey-title")  # 查找
            res = any("不保存的考试" == name.text for name in examnums)
            if res:
                print("不保存的考试——异常存在")
                TestDriver.getScreeShot()

            self.driver.fullscreen_window()
            # 2.保存考试后不开启考试 —— 清空标题名，名字设置为保存但不开启的考试，保存，回退两次到主页，再点我的项目，再用find_elemengt选中名字进入
            # 点击网址跳转到新窗口，切换窗口，显示问卷已暂停回收
            # 首页不能点击，只能back
            time.sleep(1)
            self.driver.back()  # 从我的项目回退到首页栏
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)").click())  # 创建考试
            time.sleep(1)
            self.driver.fullscreen_window()
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").clear())  # 清空考试名称
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").send_keys("保存但不开启的考试"))  # 设置考试名字
            time.sleep(2)
            (self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span").click())  # 保存考试 ———— 如果不是全屏就找不到保存元素
            self.driver.back()  # 回退到主页
            self.driver.back()  # 回退到主页
            time.sleep(2)
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li:nth-child(2) > span > a > div > span.anticon.ant-pro-base-menu-item-icon > span > svg").click())  # 点击我的项目
            time.sleep(1)
            examnums = self.driver.find_elements(By.CSS_SELECTOR, "span.survey-title")  # 查找
            name_element = None
            for name in examnums:
                if name.get_attribute("title") == "保存但不开启的考试":
                    name_element = name
                    break

            if name_element:
                name_element.click()
            else:
                print("保存但不开启的考试——异常不存在")
                TestDriver.getScreeShot()  # 截图记录

            # 不开启考试直接点击打开网址按钮
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.survey-main-content > div > div > div:nth-child(2) > div:nth-child(2) > div.ant-row > div > div > div:nth-child(1) > span.ant-input-group-wrapper.ant-input-search.ant-input-search-with-button.open-target > span > span > button > span").click()
            # 出现了新窗口，跳转到新窗口
            curwindow = self.driver.current_window_handle  # 获取当前窗口的句柄
            allwindow = self.driver.window_handles  # 获取所有窗口句柄
            for window in allwindow:
                if window != curwindow:
                    self.driver.switch_to.window(window)  # 切换到新的窗口
                    break
            # 对新窗口进行操作
            text = self.driver.find_element(By.CSS_SELECTOR, "#root > div > div > div > h2").text
            if text == "问卷已暂停回收":
                assert (text == "问卷已暂停回收")
                TestDriver.getScreeShot()  # 截图记录
            # 关闭新窗口，获取所有窗口句柄，切换回老窗口
            self.driver.close()
            allwindow = self.driver.window_handles  # 获取所有窗口句柄
            for window in allwindow:
                if window:
                    self.driver.switch_to.window(window)
                    break
                else:
                    print("异常，所有窗口已经关闭")
                    TestDriver.getScreeShot()  # 截图记录

            time.sleep(1)
            self.driver.fullscreen_window()
            time.sleep(1)
            # 3.保存考试后开启无题目的考试 —— 清空标题名，名字设置为开启但没题目的考试，保存，回退两次到主页，再点我的项目，再用find_elemengt选中名字进入，
            # 点击开启考试，点击网址跳转到新窗口，切换窗口，find_elemengts判断提交按钮不存在
            # 回退两次——从考试设置回退到我的项目——再退到首页
            self.driver.back()  # 从考试开启回退到我的项目
            self.driver.back()  # 从我的项目回退到首页栏
            time.sleep(1)
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)").click())  # 创建考试
            time.sleep(2)
            self.driver.fullscreen_window()
            time.sleep(2)
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").clear())  # 清空考试名称
            (self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p").send_keys("保存开启但无题目的考试"))  # 设置考试名字
            time.sleep(1)
            (self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span").click())  # 保存考试
            self.driver.back()  # 回退到主页
            self.driver.back()  # 回退到主页
            (self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li:nth-child(2) > span > a > div > span.anticon.ant-pro-base-menu-item-icon > span > svg").click())  # 点击我的项目
            time.sleep(1)
            examnums = self.driver.find_elements(By.CSS_SELECTOR, "span.survey-title")  # 查找
            name_element = None
            for name in examnums:
                if name.get_attribute("title") == "保存开启但无题目的考试":
                    name_element = name
                    break

            if name_element:
                name_element.click()
            else:
                print("保存开启但无题目的考试——异常不存在")
                TestDriver.getScreeShot()  # 截图记录

            # 点击开启考试按钮
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.survey-main-content > div > div > div:nth-child(2) > div:nth-child(2) > div.ant-row > div > div > div:nth-child(2) > div:nth-child(2) > button").click()
            # 点击打开网址按钮
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.survey-main-content > div > div > div:nth-child(2) > div:nth-child(2) > div.ant-row > div > div > div:nth-child(1) > span.ant-input-group-wrapper.ant-input-search.ant-input-search-with-button.open-target > span > span > button > span").click()
            # 出现了新窗口，跳转到新窗口
            curwindow = self.driver.current_window_handle  # 获取当前窗口的句柄
            allwindow = self.driver.window_handles  # 获取所有窗口句柄
            for window in allwindow:
                if window != curwindow:
                    self.driver.switch_to.window(window)  # 切换到新的窗口
                    break
            # 对新窗口进行操作
            # 找提交按钮，目标：按钮不存在
            my_list = self.driver.find_elements(By.CSS_SELECTOR,"#root > div > div > div > div.survey-antd-footer > div.survey-antd-footer-pagination > div > button > div")
            # 找不到返回空列表
            if len(my_list) > 0:
                assert (len(my_list) > 0)  # 不是空列表代表出现异常
                TestDriver.getScreeShot()  # 截图记录
            # 关闭新窗口，获取所有窗口句柄，切换回老窗口
            self.driver.close()
            allwindow = self.driver.window_handles  # 获取所有窗口句柄
            for window in allwindow:
                if window:
                    self.driver.switch_to.window(window)
                    break
                else:
                    print("异常，所有窗口已经关闭")
                    TestDriver.getScreeShot()  # 截图记录

            self.driver.back()  # 从考试开启回退到我的项目
            self.driver.back()  # 从我的项目回退到首页栏
        finally:
            TestDriver.getScreeShot()  # 截图记录

    def ExamEditCheck(self):  # 创建考试的页面检查
        self.driver.fullscreen_window()
        # 检查页面元素：标题输入框，题目：题型、大纲。提交、预览、保存
        # 题型
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-1")
        # 大纲
        self.driver.find_element(By.CSS_SELECTOR, "#rc-tabs-0-tab-3")
        # 提交
        self.driver.find_element(By.CSS_SELECTOR,"#editorContent > div > div > div.footer > div.title > div > div > pre > div.ql-editor > p")
        # 预览
        self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button:nth-child(3) > span")
        # 保存
        self.driver.find_element(By.CSS_SELECTOR,"#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span")
        # 添加截图
        TestDriver.getScreeShot()